package com.xujiang.myblog.Base;

import android.support.annotation.DrawableRes;
import android.support.annotation.MenuRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.xujiang.common.util.ToolBarHelper;
import com.xujiang.myblog.R;
import com.xujiang.myblog.util.L;

/**
 * Created by xujiang on 2017/4/21.
 *  //viewpager 对fragment会预加载后面一个fragment 这样会导致fragment的toolbar无法在底部初始化
 * 2
 */

public class BaseLazyFragment extends BaseFragment {


    public Fragment getFragment(){
        return this;
    }

    protected final String TAG = getFragment().getClass().getSimpleName();

    private ToolBarHelper toolBarHelper;

    private Toolbar toolbar;



    public void setToolbar() throws Exception {
        if(toolBarHelper == null)
            throw new Exception("请在fragment里调用initToolBarHelper方法初始化后使用");
        setToolbar(toolBarHelper.getToolbar());
    }

    public void setToolBarHelper(ToolBarHelper toolBarHelper) {
        this.toolBarHelper = toolBarHelper;
    }

    public void setToolbar(Toolbar toolbar) {
        this.toolbar = toolbar;
    }

    public Toolbar getToolbar() {
        return toolBarHelper.getToolbar();
    }


    public ToolBarHelper getToolBarHelper() {
        return toolBarHelper;
    }




    protected ToolBarHelper initToolBar(View view) {
        if (view == null) {
            return null;
        }
        this.toolBarHelper = ToolBarHelper.with(view);
        return toolBarHelper;
    }

    public void setNavigationIcon(int Resid) throws Exception {
        if(toolbar == null)
            setToolbar();
        toolbar.setNavigationIcon(Resid);
    }

    public void setLogo(int Resid) throws Exception {
        if(toolbar == null)
            setToolbar();
        toolbar.setLogo(Resid);
    }

    public void setTitleAndSubTitle(String title,String subtitle) throws Exception {
        if(toolbar == null)
            setToolbar();
        toolbar.setTitle(title);
        toolbar.setSubtitle(subtitle);
    }

    public void seMenu(@MenuRes int resId) throws Exception {
        if(toolbar == null)
            setToolbar();
        toolbar.inflateMenu(resId);
    }
    /**
     *
     * @param resId1   导航栏图标
     * @param resId2   logo
     * @param resId3   标题
     * @param title    小标题
     * @param subtitle  菜单
     * id 如果不设置 请传0
     *
     * */
    public void setAll(@Nullable Toolbar mtoolbar, @DrawableRes int resId1, @DrawableRes int resId2, CharSequence title, CharSequence subtitle, @MenuRes int resId3) throws Exception {
        if (mtoolbar != null)
            toolbar = mtoolbar;
        if(toolbar == null)
            setToolbar();
        if (0 != resId1) {
            toolbar.setNavigationIcon(resId1);
        }
        if(0 != resId2){
            toolbar.setLogo(resId2);
        }
        if(title != null){
            toolbar.setTitle(title);
        }
        if(subtitle != null){
            toolbar.setSubtitle(subtitle);
        }
        if(0 != resId3){
            toolbar.inflateMenu(resId3);
        }

    }

    public void setDefaultOnclicklistener(Toolbar mtoolbar){
        if (mtoolbar != null)
            toolbar = mtoolbar;
        toolbar.setNavigationOnClickListener(v -> {
            L.getToast().showLog(getContext(),"setNavigationOnClickListener 在BaseLazyFragment的test");});
        toolbar.setOnClickListener( v -> {
            L.getToast().showLog(getContext(),"setOnClickListener 在BaseLazyFragment的test");
        });

        toolbar.setOnMenuItemClickListener(item -> {
            switch (item.getItemId()) {
                case R.id.action_search:
                    Toast.makeText(getContext(), "action_search", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_notification:
                    Toast.makeText(getContext(), "action_notification", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.action_item1:
                    Toast.makeText(getContext(), "action_item1", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.action_item2:
                    Toast.makeText(getContext(), "action_item2", Toast.LENGTH_SHORT).show();

                    break;
            }

            return true;
        });
    }






}
